package cn.micro.dc_excel.tools;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;

public class ToolsDaochu {
    public static void getTfAttenceListExport(HttpServletRequest req, HttpServletResponse res, String xlsName, HashMap<String, Object> map) throws Exception {
        WritableSheet ws;
        OutputStream os = res.getOutputStream();// 取得输出流
        res.reset();// 清空输出流
        res.setHeader("Content-Disposition", "attachment;filename=" + new String((xlsName + ".xls").getBytes("gb2312"), "iso-8859-1"));
        res.setContentType("application/msexcel");// 定义输出类型
        WritableWorkbook wbook = Workbook.createWorkbook(os);
        ws = wbook.createSheet("shell", 0);
        WritableFont Titlefont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD,
                false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        WritableCellFormat tilefotmat = new WritableCellFormat(Titlefont);
        //Label(列，行，数值)
        for (String k : map.keySet()) {
            String[] s = k.split("-");
            int y = Integer.parseInt(s[0]);
            int x = Integer.parseInt(s[1]);
            String v;
            if (map.get(k) == null) {
                v = "";
            } else {
                v = map.get(k).toString();
            }
            ws.addCell(new Label(x, y, v));
            ws.setColumnView(y, 15);
        }
        wbook.write(); // 写入文件
        wbook.close();
        os.close(); // 关闭流
    }

}
